package com.telnetnz.ticketdesk.action.common;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.log4j.Logger;

import com.rockbb.common.simpleframe.action.MultipartFile;
import com.rockbb.common.utilities.i18n.Language;
import com.rockbb.common.utilities.i18n.MessageResource;
import com.rockbb.common.utilities.text.ComponentBuilder;
import com.telnetnz.ticketdesk.bean.Project;
import com.telnetnz.ticketdesk.bean.Tag;
import com.telnetnz.ticketdesk.bean.User;
import com.telnetnz.ticketdesk.service.UserService;

public class CommonUtils
{
	private static Logger logger = Logger.getLogger(CommonUtils.class);
	
	/**
	 * Generate the order-by drop down list
	 * 
	 * @param langRes can be null
	 * @param langKey can be null
	 * @param orderbyArray
	 * @param orderby
	 * @return
	 */
	public static String buildOrderByDropdown(MessageResource langRes, String langKey, String[] orderbyArray, int orderby)
	{
		String[] values = new String[orderbyArray.length];
		String[] names = new String[orderbyArray.length];
		for (int i=0; i<orderbyArray.length; i++)
		{
			values[i] = ""+i;
			names[i] = (langKey == null)? orderbyArray[i] : (langKey+ i);
		}

		return ComponentBuilder.buildOptions(langRes, values, names, orderby);
	}

	/**
	 * Generate the DESC or ASC drop down list
	 * 
	 * @param langRes can be null
	 * @param langKey can be null
	 * @param orderArray
	 * @param order
	 * @return
	 */
	public static String buildOrderDropdown(MessageResource langRes, String langKey, String[] orderArray, int order)
	{
		String[] values = new String[orderArray.length];
		String[] names = new String[orderArray.length];
		for (int i=0; i<orderArray.length; i++)
		{
			values[i] = ""+i;
			names[i] = (langKey == null)? orderArray[i] : (langKey+ i);
		}
		return ComponentBuilder.buildOptions(langRes, values, names, order);
	}

	/**
	 * Generate the page sizes drop down list
	 * 
	 * @param sizeArray
	 * @param size
	 * @return
	 */
	public static String buildPageSizeDropdown(int[] sizeArray, int size)
	{
		String[] values = new String[sizeArray.length];
		String[] names = new String[sizeArray.length];
		for (int i=0; i<sizeArray.length; i++)
		{
			values[i] = ""+sizeArray[i];
			names[i] = ""+sizeArray[i];
		}
		return ComponentBuilder.buildOptions(null, values, names, size+"");
	}

	public static String buildYearDropdown(int start, int end, int year)
	{
		String[] values = new String[end - start];
		for (int i=0; i<(end - start); i++)
		{
			values[i] = ""+ (start + i);
		}
		return ComponentBuilder.buildOptions(null, values, values, year+"");
	}

	/**
	 * Generate the status drop down list
	 * 
	 * @param langRes
	 * @param langKey
	 * @param statusArray
	 * @param status
	 * @param allowEmpty
	 * @return
	 */
	public static String buildSingleDropdown(MessageResource langRes, String langKey, String[] statusArray, int status, boolean allowEmpty)
	{
		String[] values = new String[statusArray.length];
		String[] names = new String[statusArray.length];
		for (int i=0; i<statusArray.length; i++)
		{
			values[i] = ""+i;
			names[i] = (langKey == null)? statusArray[i] : (langKey+ i);
		}
		if (allowEmpty)
		{
			values = ArrayUtils.add(values, 0, "-1");
			names = ArrayUtils.add(names, 0, (langKey == null)? "" : (langKey+ "-1"));
		}
		return ComponentBuilder.buildOptions(langRes, values, names, status+"");
	}

	public static String buildLanguageDropdown(List<Language> languages, int language, boolean allowEmpty)
	{
		String[] names = new String[languages.size()];
		String[] values = new String[languages.size()];
		for (int i = 0; i < languages.size(); i++)
		{
			names[i] = languages.get(i).getLocalName();
			values[i] = i+"";
		}
		if (allowEmpty)
		{
			values = ArrayUtils.add(values, 0, "-1");
			names = ArrayUtils.add(names, 0, "");
		}
		return ComponentBuilder.buildOptions(null,values,names,language+"");
	}

	public static String buildLanguageDropdown(List<Language> languages, List<Language> sel_languages, boolean allowEmpty)
	{
		String[] names = new String[languages.size()];
		String[] values = new String[languages.size()];
		for (int i = 0; i < languages.size(); i++)
		{
			names[i] = languages.get(i).getLocalName();
			values[i] = i+"";
		}
		if (allowEmpty)
		{
			values = ArrayUtils.add(values, 0, "-1");
			names = ArrayUtils.add(names, 0, "");
		}
		String[] str_languages = new String[sel_languages.size()];
		for (int i = 0; i < sel_languages.size(); i ++)
		{
			str_languages[i] = sel_languages.get(i).getId() + "";
		}
		return ComponentBuilder.buildOptions(null, values, names, str_languages);
	}

	public static String buildProjectDropdown(List<Project> projects, int projectId, boolean allowEmpty)
	{
		String[] names = new String[projects.size()];
		String[] values = new String[projects.size()];
		for (int i = 0; i < projects.size(); i++)
		{
			names[i] = projects.get(i).getName();
			values[i] = projects.get(i).getId()+"";
		}
		if (allowEmpty)
		{
			values = ArrayUtils.add(values, 0, "0");
			names = ArrayUtils.add(names, 0, "--");
		}
		return ComponentBuilder.buildOptions(null,values,names,projectId+"");
	}

	public static String buildTagDropdown(List<Tag> tags, long tagId, boolean allowEmpty)
	{
		String[] names = new String[tags.size()];
		String[] values = new String[tags.size()];
		for (int i = 0; i < tags.size(); i++)
		{
			names[i] = tags.get(i).getName();
			values[i] = tags.get(i).getId()+"";
		}
		if (allowEmpty)
		{
			values = ArrayUtils.add(values, 0, "0");
			names = ArrayUtils.add(names, 0, "");
		}
		return ComponentBuilder.buildOptions(null, values, names, tagId+"");
	}

	public static String buildTagDropdown(List<Tag> tags, List<Tag> selectedTags, boolean allowEmpty)
	{
		String[] names = new String[tags.size()];
		String[] values = new String[tags.size()];
		for (int i = 0; i < tags.size(); i++)
		{
			names[i] = tags.get(i).getName();
			values[i] = tags.get(i).getId()+"";
		}
		if (allowEmpty)
		{
			values = ArrayUtils.add(values, 0, "0");
			names = ArrayUtils.add(names, 0, "");
		}
		String[] str_tags = new String[selectedTags.size()];
		for (int i = 0; i < selectedTags.size(); i ++)
		{
			str_tags[i] = selectedTags.get(i).getId() + "";
		}
		return ComponentBuilder.buildOptions(null, values, names, str_tags);
	}

	/**
	 * Generate the internal user drop down list
	 * 
	 * @param uid
	 * @return
	 */
	public static String buildUserDropdown(List<User> userList, long id, boolean allowEmpty)
	{
		String[] names = new String[userList.size()];
		String[] values = new String[userList.size()];
		for (int i = 0; i < userList.size(); i++)
		{
			names[i] = userList.get(i).getName();
			values[i] = userList.get(i).getId()+"";
		}
		if (allowEmpty)
		{
			values = ArrayUtils.add(values, 0, "0");
			names = ArrayUtils.add(names, 0, "");
		}
		return ComponentBuilder.buildOptions(null, values, names, id+"");
	}

	public static String buildTimezoneDropdown(MessageResource langRes, float timezone)
	{
		String[] values = new String[UserService.USER_TIMEZONE_KEYS.length];
		String[] names = new String[UserService.USER_TIMEZONE_KEYS.length];
		for (int i=0; i<UserService.USER_TIMEZONE_KEYS.length; i++)
		{
			values[i] = String.format("%.2f", UserService.USER_TIMEZONE_KEYS[i]);
			names[i] = "global.timezone."+String.format("%.2f", UserService.USER_TIMEZONE_KEYS[i]);
		}

		return ComponentBuilder.buildOptions(langRes, values, names, String.format("%.2f", timezone));
	}

	/**
	 * 上传图片
	 * @param input 传入文件流
	 * @param filePath 目的文件路径
	 * @return
	 */
	public static boolean uploadFile(MultipartFile inputFile, String filePath)
	{
		if(inputFile == null || filePath == null || filePath.equals(""))
		{
			return false;
		}

		try
		{
			String folderPath = filePath.substring(0, filePath.lastIndexOf("/")+1);
			File folder = new File(folderPath);
			if(!folder.exists())
			{
				folder.mkdirs();
			}

			File file = new File(filePath);
			FileUtils.copyInputStreamToFile(inputFile.getInputStream(), file);
			return true;
		}
		catch(Exception e)
		{
			logger.info("[Error] File upload failed when creating " + filePath);
			logger.debug(e.getMessage(), e);
		}
		return false;
	}

}
